#include <iostream>
#include <pthread.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>


using namespace std;


void out_state(pthread_attr_t *attr){
    int state;
    if(pthread_attr_getdetachstate(attr,&state)!=0){
        perror("getdetachstate error");
    }else{
        if(state==PTHREAD_CREATE_JOINABLE){
            std::cout<<"joinable create"<<std::endl;
        }else if(state==PTHREAD_CREATE_DETACHED){
            std::cout<<"detached state"<<std::endl;
        }else{

            std::cout<<"error state"<<std::endl;

        }


    }

}


void* func(void*arg){

    int i=0;
    int sum=0;
    for(i=1;i<=100;i++){

        sum+=1;

    }


    return (void*)sum;
}



int mian(){
    int err;

    pthread_t t1,t2;

    //定义线程属性
    pthread_attr_t attr;

    //对线程属性初始化
    pthread_attr_init(&attr);

    //输出分离属性
    out_state(&attr);

    //取分离属性的默认值，以正常方式启动子线程
    if((err=pthread_create(&t1,&attr,func,(void*)0))!=0){
        perror("pthread create error");

    }
    int res;
#if 0
    if((err=pthread_join(t1,(void*)&res))!=0){
        perror("pthread join error");
    }else{
        std::cout<<"default return is"<<(int)res<<std::endl;


    }

#endif
    //销毁线程属性
    pthread_attr_destroy(&attr);
    



    return 0;
}
